//
//  ZWLSqrureCell.swift
//  ZWLDY
//
//  Created by czh on 2016/10/12.
//  Copyright © 2016年 zwl. All rights reserved.
//

import UIKit

private let cellID = "cellID"

class ZWLSqrureCell: UICollectionViewCell {
    
    @IBOutlet weak var collectionView: UICollectionView!
    var groups : [AnchorGroup]? {
        didSet {
            collectionView.reloadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        
        // 注册 cell
        collectionView.register( UINib(nibName: "CollectionGameCell", bundle: Bundle.main), forCellWithReuseIdentifier: cellID)
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 设置 item 大小
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        let itemW = collectionView.bounds.width / 4
        let itemH = collectionView.bounds.height / 2
        
//        layout.itemSize = CGSize(width: 80, height: 90)
        layout.itemSize = CGSize(width: itemW, height: itemH)
    }

}

extension ZWLSqrureCell : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return groups?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // 1.求出Cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! CollectionGameCell
        
        // 2.给Cell设置数据
        cell.group = groups![indexPath.item]
        cell.clipsToBounds = true
        
        return cell
    }
    
}
